package com.jic.member.impl.power;
import	java.util.ArrayList;

import java.lang.Long;
import java.util.List;


import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.manager.request.ChildNameRequest;
import com.jic.member.entity.power.MememberTypeGrade;
import com.jic.member.mapper.power.MememberTypeGradeMapper;
import com.jic.member.service.power.MememberTypeGradeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 会员类型等级记录表
 * @author : 
 * @email : 
 * @since : 2020-04-27 02:59:22
 * @version : v1.0.0
 */
@Service
public class MememberTypeGradeServiceImpl implements MememberTypeGradeService {

    @Resource
    private MememberTypeGradeMapper mememberTypeGradeMapper;
    @Resource
    private SysDeptApi sysDeptService;

    @Override
    public long insert(MememberTypeGrade mememberTypeGrade) {
        long flag = mememberTypeGradeMapper.insert(mememberTypeGrade);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mememberTypeGradeMapper.delete(id);
    }

    @Override
    public long update(MememberTypeGrade mememberTypeGrade) {
        return mememberTypeGradeMapper.update(mememberTypeGrade);
    }

    @Override
    public MememberTypeGrade queryByPrimaryKey(Long id){
        return mememberTypeGradeMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MememberTypeGrade> query(MememberTypeGrade mememberTypeGrade){
        return mememberTypeGradeMapper.query(mememberTypeGrade);
    }

    @Override
    public PageResult<MememberTypeGrade> queryPage(MememberTypeGrade mememberTypeGrade, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        AdminOperate adminOperate = mememberTypeGrade.getAdminOperate();
        List<Long> deptTypes = new ArrayList<> ();
        //母平台登录查询全部
        if(adminOperate.getDeptType().getCode() == 0){
            mememberTypeGrade.setDeptType(null);
        }else if (adminOperate.getDeptType().getCode() == 1){
            deptTypes.add(1L);
            deptTypes.add(2L);
            deptTypes.add(3L);
        }else if (adminOperate.getDeptType().getCode() == 2){
            deptTypes.add(2L);
            deptTypes.add(3L);
        }else if (adminOperate.getDeptType().getCode() == 3){
            deptTypes.add(3L);
        }
        mememberTypeGrade.setDeptTypes(deptTypes);
        List<MememberTypeGrade> list = mememberTypeGradeMapper.queryPage(startIndex, pageQuery.getLimit(), mememberTypeGrade);
        long count = mememberTypeGradeMapper.queryPageCount(mememberTypeGrade);
        list.forEach(item->{
            RestResult<String> name = sysDeptService.queryDeptNameById(item.getDeptId());
            ChildNameRequest childNameRequest = new ChildNameRequest();
            childNameRequest.setDeptId(item.getDeptId());
            RestResult<String> dept =sysDeptService.queryChildNameByDeptId(childNameRequest);
            item.setDeptName(name.getData());
            item.setParentName(dept.getData());
        });
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }
}